<?php

/**
 * This is the model class for table "partner".
 *
 * The followings are the available columns in table 'partner':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property string $title
 * @property string $city_id
 * @property string $bank_name
 * @property string $bank_no
 * @property string $bank_user
 * @property string $location
 * @property string $contact
 * @property string $phone
 * @property string $address
 * @property string $other
 * @property string $mobile
 * @property string $enable
 * @property string $user_id
 * @property string $create_time
 *
 * The followings are the available model relations:
 */
class Partner extends CActiveRecord
{

	/**
	 * Returns the static model of the specified AR class.
	 * @return Partner the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'partner';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username,password,repassword,title,contact,phone', 'required'),
			array('username, password, contact, repassword', 'length', 'max'=>32),
			array('title, bank_name, bank_no, bank_user, address', 'length', 'max'=>128),
			array('city_id, user_id, create_time', 'length', 'max'=>10),
			array('phone', 'length', 'max'=>18),
			array('mobile', 'length', 'max'=>12),
			array('enable', 'length', 'max'=>1),
			array('other', 'safe'),
			array('location', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('password','checkPwd'),
			array('id, username, password, title, city_id, bank_name, bank_no, bank_user, location, contact, phone, address, other, mobile, enable, user_id, create_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'city'=>array(self::BELONGS_TO,'Category','city_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => '用户名',
			'password' => '密码',
			'repassword' => '确认密码',
			'title' => '商户名',
			'city_id' => '所在城市',
			'bank_name' => '开户行名称',
			'bank_no' => '银行账号',
			'bank_user' => '银行用户名',
			'location' => '位置信息',
			'contact' => '联系人',
			'phone' => '电话号码',
			'address' => '地址',
			'other' => '其他',
			'mobile' => '手机号码',
			'enable' => '可否消费',
			'user_id' => 'User_id',
			'create_time' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('city_id',$this->city_id,true);
		$criteria->compare('bank_name',$this->bank_name,true);
		$criteria->compare('bank_no',$this->bank_no,true);
		$criteria->compare('bank_user',$this->bank_user,true);
		$criteria->compare('location',$this->location,true);
		$criteria->compare('contact',$this->contact,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('other',$this->other,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('enable',$this->enable,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('create_time',$this->create_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
    public $repassword;
    function checkPwd()
    {
    	if($this->password!=$this->repassword){
            $this->addError('password','密码错误');
            $this->addError('repassword','两次密码不一致');
    	}
	}

	protected function beforeSave()
	{
			$this->create_time=time();
            $this->password = md5($this->password);
        return true;
	}

	 public function validatePassword($password)
    {
        return $this->hashPassword($password)===$this->password;
    }

    public function hashPassword($password)
    {
        return md5($password);
    }



	private static $_items=array();
	public function partnername()
	{
		$models=self::model()->findAll(
		//array(//'condition'=>'type=:name',
			//'params'=>array(':type'=>name),
			//)
		);
		foreach($models as $model)
			self::$_items[$model->id]=$model->username;


		return self::$_items;

	}
   }